﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class PublicationController : ContributionController
    {
        #region Attributes
        private PublicationBLL publicationBLL;
        private SubjectBLL subjectBLL;
        private ContributionItemBLL contributionItemBLL;
        #endregion

        #region Constructor

        public PublicationController()
        {
            publicationBLL = new PublicationBLL();
            subjectBLL = new SubjectBLL();
            contributionItemBLL = new ContributionItemBLL();
        }

        public PublicationController(PublicationBLL publicationBLL)
        {
            this.publicationBLL = publicationBLL;
        }
        #endregion

        #region Create


        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Publication publication)
        {
            publication.AcademicPerson = new AcademicPersonBLL().RetrieveAcademicPersonByID(Convert.ToInt32(CurrentUser.PersonID.Value));
            publicationBLL.Create(publication);
            return RedirectToAction("Index");
        }

        #endregion

        #region Edit

        [HttpGet]
        public ActionResult Edit(String id)
        {
            Session["id"] = id;
            var publication = publicationBLL.RetrievePublication(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));

            TempData["Subjects"] = subjectBLL.RetrieveSubjects(publication.ContributionID);
            TempData["ContributionItems"] = contributionItemBLL.RetrieveContributionItems(publication.ContributionID);

            return View(publication);
        }

        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "ContributionID")] Publication publication)
        {
            if (!ModelState.IsValid)
                return View();

            var id = Convert.ToInt32(URLEncrypt.DecryptFromString(Session["id"].ToString()));
            var originalPublication = publicationBLL.RetrievePublication(id);

            originalPublication.Description = publication.Description;
            originalPublication.Title = publication.Title;
            originalPublication.SourceInfo = publication.SourceInfo;
            originalPublication.RegisterDate = publication.RegisterDate;

            publicationBLL.Edit(originalPublication);
            return RedirectToAction("Index");
        }
        #endregion

        #region MyIndex
        [HttpGet]
        public ActionResult MyIndex()
        {
            ViewData["GridSource"] = publicationBLL.RetrievePublications(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }
        [HttpGet]
        public ActionResult Index()
        {
            ViewData["GridSource"] = publicationBLL.RetrievePublications();
            return View();
        }
        #endregion
        [HttpGet]
        public ActionResult Visualize(string id)
        {
            var entity = publicationBLL.RetrievePublication(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }
    }


}

